﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Data;

namespace SharePointProject3.Layouts.SharePointProject3
{
    public class PhoneBookEntry
    {
        public string Company { get; set; }
        public string Surname { get; set; }
        public string Name { get; set; }
        public string Patronumic { get; set; }
        public string Title { get; set; }
        public string Telephone { get; set; }
    }
    public partial class ApplicationPage1 : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e) { }
        public void Submit_Click(object sender, EventArgs e)
        {
            if (Input.Text == "ОК")
            {
                using (SPSite site = new SPSite("http://pinchuk"))
                using (SPWeb web = site.OpenWeb("/"))
                {

                    string listUrl = "Lists/List";
                    SPList list = web.GetList(listUrl);
                    GreadView.DataSource = GetPhoneBookEntrys(list.GetItems("Company", "Surname", "Name", "Patronymic", "Title", "Surname", "Telephone"));
                }
            }
            else
                Information.Text = "Error DATA";
        }
        public List<PhoneBookEntry> GetPhoneBookEntrys(SPListItemCollection collection)
        {
            var res = new List<PhoneBookEntry>();
            try
            {
                foreach (SPListItem item in collection)
                    res.Add(new PhoneBookEntry()
                    {
                        Company = item["Company"],
                        Surname = item["Surname"],
                        Name = item["Name"],
                        Patronumic = item["Patronymic"],
                        Title = item["Title"],
                        Telephone = item["Telephone"],
                    });
            }
            catch { }
            return res;
        }
    }
}
